home *** CD-ROM | disk | FTP | other *** search
/ Technotools / Technotools (Chestnut CD-ROM)(1993).ISO / lang_c / environ / readfile.c < prev    next >
C/C++ Source or Header  |  1988-01-01  |  4KB  |  98 lines

  1. /*  use msc readfile /AL; link readfile environ getenv2;
  2.     readfile.c
  3.  
  4.     This program impliments a read command for batch files.
  5.     What READFILE does is re-open the text file who's name was stored in
  6.     the environment string, and read the next line from it.  The postion in
  7.     the file is updated and stored back in the environment string, so
  8.     subsequent calls to READFILE will return successive lines from the
  9.     file.  The file gets opened over and over again for each line,but you
  10.     get all the advantages of batch files with little programming.
  11.     To use READFILE in a batch file type:
  12.     
  13.     READFILE <handle> <stringname>
  14.     
  15.     handle is the unique identifer you gave to OPENFILE.
  16.     stringname is the name of a string to store the text in the
  17.     environment.
  18.     
  19.     READFILE returns errorlevel=0 for failure, 1 for success
  20. */
  21.  
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <dos.h>
  25. #include <string.h>
  26.  
  27. extern char *search_for();    /*searches environment for string*/
  28. extern del_string();        /*deletes a string from the environment*/
  29. extern int ins_string();    /*insert a new string in the environment*/
  30.  
  31. char name[128]="n_";    /*space for the file name string*/
  32. char pos[64]="p_";      /*space for the file position string*/
  33. char line[200];         /*space for the line read from file*/
  34.  
  35. int envseg;        /*segment of environment string*/
  36. char far *env;        /*pointer to environment string*/
  37. extern unsigned int far getenv2();    /* routine to find environment and its size*/
  38. int enl;        /*length of environment in bytes*/
  39.  
  40. main(argc,argv)    
  41. int argc;
  42. char *argv[];
  43. {
  44.     int    arg=1;        /*where to look for arguments*/
  45.     char *ptr;        /*scratch pointer for scanning around*/
  46.     FILE *fh;        /*file handle to read with*/
  47.     long posit;        /*position in file*/
  48.     int    l;        /*scratch for string lengths*/
  49.  
  50.     if (argc<2)        /*do I have enough arguments?*/
  51.     {
  52.     printf("Usage: READ <handle> <pathname>\r\n");
  53.     exit(0);    /*exit if no file name*/
  54.     }
  55.     if (argc<3)        /*if there is only one argument,*/
  56.     arg=0;        /*use it as the file name*/
  57.     enl=getenv2(&envseg);    /*get length and address of system environment*/
  58.     FP_SEG(env)=envseg;
  59.     FP_OFF(env)=0;
  60.     strcat(name,argv[arg]);    /*build the file name string*/
  61.     strcat(name,"=");
  62.     ptr=search_for(name);    /*is there a file for this handle?*/
  63.     if (ptr == NULL)
  64.     {
  65.     printf("You did not OPEN a file first\r\n");
  66.     exit(0);
  67.     }
  68.     fh=fopen((ptr+=strlen(name)),"r");    /*open the file*/
  69.     if (fh == NULL)            /*abort if you cannot open it*/
  70.     {
  71.     printf("Cannot open file %s\r\n",ptr);
  72.     exit(0);
  73.     }
  74.     strcat(pos,argv[arg]);    /*build the file position string*/
  75.     strcat(pos,"=");
  76.     ptr=search_for(pos);    /*find address of position string*/
  77.     if (ptr == NULL)
  78.     {
  79.     printf("There is something seriously wrong in environment!\r\n");
  80.     exit(0);
  81.     }
  82.     sscanf(ptr+strlen(pos),"%ld",&posit);    /*get file position*/
  83.     fseek(fh,posit,0);                /*go there*/
  84.     strcpy(line,argv[arg+1]);        /*build the text string name*/
  85.     strcat(line,"=");
  86.     del_string(line);            /*deletae any old string same name*/
  87.     if (fgets(line+strlen(line),180,fh)== NULL) /*read a line of text*/
  88.     exit(0);        /*must have hit eof*/
  89.     del_string(pos);            /*delete the string position*/
  90.     posit=ftell(fh);            /*find out where we are in the file*/
  91.     sprintf(pos+strlen(pos),"%ld",posit);    /*now and convert to string*/
  92.     ins_string(pos);            /*store back in environment*/
  93.     line[strlen(line)-1]=0;        /*chop the newline off the end*/
  94.     ins_string(line);            /*add it to the environment*/
  95.     fclose(fh);
  96.     exit(1);
  97. }
  98.